package com.topisv.tms.web.work;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.topisv.tms.entity.JSONParam;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.service.vo.invoice.InvoiceRequest;
import com.topisv.tms.service.vo.invoice.InvoiceReturnModel;
import com.topisv.tms.service.work.InvoicePrintService;
import com.topisv.tms.service.work.WorkOrderService;
import com.topisv.tms.service.work.WorkTaskService;
import com.topisv.tms.utils.WmsContants;
//import com.topisv.tms.utils.http.SynchroShippedBatchQueue;
import com.topisv.tms.web.base.BaseDataTableController;

/**
 * 发票打印controller
 *  Date          Author       Version       Description
 * ----------------------------------------------------------
 *  2015-1-13      CC            1.0           Create
 */
@Controller
@RequestMapping("/v2/invoicePrint")
public class InvoicePrintController extends BaseDataTableController {
	@Autowired
	private WorkTaskService workTaskService;
	@Autowired
	private WorkOrderService workOrderService;
	@Autowired
	private InvoicePrintService invoicePrintService;

	@RequestMapping(value = "show", method = RequestMethod.GET)
	public String show(Model model) {
		fillSelectValue(model, null);
		return "work/invoicePrint/invoicePrintList";
	}

	@RequestMapping(value = "page", method = RequestMethod.POST)
	public void page(@RequestBody JSONParam[] paramJson, HttpServletRequest request, HttpServletResponse response) {
		try {
			// 客户端参数
			QueryParamHolder paramHolder = collectParam(paramJson);
			Map<String, Object> searchParams = paramHolder.getSearchParams();
			searchParams.put("EQ_needInvoice", "1");// 需要打印发票的任务
			searchParams.put("EQ_customerCode", WmsContants.CUSTOMER_MID);// 只查询美的
			String fromTm = (String) searchParams.get("GTE_createTime");
			String toTm = (String) searchParams.get("LT_createTime");
			String orderNo = (String) searchParams.get("EQ_order.ordeno");
			String taskCode = (String) searchParams.get("EQ_taskCode");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (!StringUtils.isBlank(fromTm)) {
				searchParams.put("GTE_createTime", sdf.parse(fromTm));
			}
			if (!StringUtils.isBlank(toTm)) {
				searchParams.put("LT_createTime", sdf.parse(toTm));
			}
			Page<?> page = null;
			if(StringUtils.isEmpty(orderNo)&&StringUtils.isEmpty(taskCode)){
				page = workTaskService.findByJson(paramHolder);
			}else{
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("orderno", orderNo);
				param.put("taskCode", taskCode);
				page = workOrderService.loadPageOrder(param, paramHolder.getCurPage(), paramHolder.getPageSize());
			}
			// 封装查询结果
			response.getWriter().print(bulidJSON(page));
		} catch (Exception e) {
			log.error("query case error.", e);
		}
	}
	@RequestMapping(value = "spliteInvoiceOrder", method = RequestMethod.POST)
	public void spliteInvoiceOrder(HttpServletRequest request, HttpServletResponse response, 
			@RequestParam(value = "taskCode") String taskCode,
			@RequestParam(value = "size") int size) {
		PrintWriter out = null;
		JSONObject json = null;
		try {
			log.info("开始分割订单号");
			log.info("taskCode===" + taskCode + "\tsize=" + size);
			json = new JSONObject();
			out = response.getWriter();
			String content = invoicePrintService.spliteInvoiceOrder(taskCode, size);
			json.put("status", "1");
			json.put("content", content);
			log.info("content==="+content);
			out.print(json.toString());
		} catch (Exception e) {
			
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			log.error("打印加载错误", e);
		}
	}
	
	
	@RequestMapping(value = "printInvoice", method = RequestMethod.POST)
	public void printInvoice(HttpServletRequest request, HttpServletResponse response, InvoiceRequest xmlRequest) {
		PrintWriter out = null;
		JSONObject json = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			List<String> content = invoicePrintService.buildInvoiceXml(xmlRequest);
			json.put("status", "1");
			json.put("content", content);
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			log.error("打印加载错误", e);
		}
	}
	@RequestMapping(value = "rePrintInvoiceByTaskCode/{taskCode}", method = RequestMethod.GET)
	public String rePrintInvoiceByTaskCode(HttpServletRequest request, HttpServletResponse response,@PathVariable("taskCode") String taskCode,Model model) {
		fillSelectValue(model, null);
		model.addAttribute("orderList", workOrderService.findByTaskCode(taskCode));
		return "work/invoicePrint/reInvoicePrint";
	}
	@RequestMapping(value = "rePrintInvoiceByOrderNo/{orderNo}", method = RequestMethod.GET)
	public String rePrintInvoiceByOrderNo(HttpServletRequest request, HttpServletResponse response,@PathVariable("orderNo") String orderNo,Model model) {
		fillSelectValue(model, null);
		model.addAttribute("orderList", Arrays.asList(workOrderService.loadOrderByOrderno(orderNo)));
		return "work/invoicePrint/reInvoicePrint";
	}

	@RequestMapping(value = "changePrintInvoiceStatus", method = RequestMethod.POST)
	public void changePrintInvoiceStatus(HttpServletRequest request, HttpServletResponse response, InvoiceReturnModel invoiceReturn) {
		PrintWriter out = null;
		JSONObject json = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			invoiceReturn.setUser(getShiroUser());
//			SynchroShippedBatchQueue.putInvoicePrinterQueue(invoiceReturn);
			json.put("status", "1");
			json.put("content", "修改成功");
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			log.error("打印加载错误", e);
		}
	}
}
